package com.jia.acl.config;

import com.jia.acl.entity.Permission;
import com.jia.acl.filter.JWTLoginFilter;
import com.jia.acl.filter.JWTValidationFilter;
import com.jia.acl.service.PermissionService;
import com.jia.acl.service.impl.MemberDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;

import java.util.List;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    MemberDetailsService memberDetailsService;

    @Autowired
    PermissionService permissionService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication()
//                .withUser("admin")
//                .password(passwordEncoder().encode("123"))
//                .authorities("admin");
//        auth.inMemoryAuthentication()
//                .withUser("jia")
//                .password(passwordEncoder().encode("123"))
//                .authorities("get","add");
        auth.userDetailsService(memberDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //http.formLogin().loginProcessingUrl("/login");
        http.addFilter(new JWTValidationFilter(authenticationManager()))
                .addFilter(new JWTLoginFilter(authenticationManager()))
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); //剔除session

        List<Permission> permissionList = permissionService.list();
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry = http.authorizeRequests();

        permissionList.forEach(permission->{
            if (!StringUtils.isEmpty(permission.getValue()) && !StringUtils.isEmpty(permission.getPath())){
                expressionInterceptUrlRegistry
                        .antMatchers(permission.getPath())
                        .hasAnyAuthority(permission.getValue());
            }
        });
        expressionInterceptUrlRegistry
                .antMatchers("/auth/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .csrf().disable();
    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder;
    }
}
